import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class AppService implements ApplicationContextAware {
  def servletContext

  void setApplicationContext(ApplicationContext context) { this.servletContext = context.getServletContext() }

  boolean transactional = true

  def say(){
    println "AppService say hello"
  }
  def initCountry() {
    if (Country.count() == 0) {
      servletContext.getResourceAsStream("/res/country.txt").eachLine {line ->
        println line
        def fields = line.split(" 	")
        if (fields.size() == 5)
          new Country(enName: fields[0], chName: fields[1], phoneCode: fields[3]).save()
        else
          new Country(enName: fields[0], chName: fields[1], phoneCode: fields[2]).save()
      }
    }
  }

  def initCity() {
    if (City.count() == 0) {
      def province = null
      servletContext.getResourceAsStream("/res/city.txt").eachLine {line ->
        println line
        def fields = line.split(" ")
        if (fields.size() == 2)
          province = fields[0]
        else if (fields.size() == 3)
          new City(chName: fields[0], enName: fields[1], countryCode: '86', cityCode: fields[2], provinceName: province).save()
        else
          println "ERROR:" + line
      }
    }
  }

  def initCategory() {
    def knowledge = new Catalog(name: 'knowledge', description: 'knowledge').save()
    def expert = new Catalog(name: 'expert', description: 'expert', parent: knowledge).save()
    def caseCate = new Catalog(name: 'case', description: 'case').save()
  }

  def initMilestone(){
    def milestone = Catalog.findByName("milestone")
    if(!milestone)       {
      milestone = new Catalog(name:'milestone', description:'milestone').save()
      boolean newOne = true;
      def title = null
      def content = new StringBuffer()
      servletContext.getResourceAsStream("/res/milestone.txt").eachLine {line ->
        if(line.trim().size() != 0){
          if(newOne){
            title = line
            newOne = false
          } else {
            content << line << "<br/>"
          }
        } else {
          newOne = true
          if(title && content.size() > 0){
            def entry = new Entry(title:title, content:content.toString(), catalog:milestone)
            entry.save()
            if(entry.errors) println entry.errors
            title = null
            content = new StringBuffer()
          }
        }

      }
    }
    
  }

  def initPlan(){
    def count = Plan.count()
    if(count == 0){
      servletContext.getResourceAsStream("/res/plan.csv").eachLine {line ->
        def fields = line.split(',')
        if(fields.size() == 4){
          String name = fields[0]
          Integer start = new Integer(fields[1])
          Integer end = new Integer(fields[2])
          String description = fields[3]
          def plan = new Plan(name:name, startWith:start,endWith:end,description:description, planType:Plan.PLAN_TYPE_SHOULD_CARE).save()
        }       
      }
    }
  }

}
